package com.cuiweiyou.happypaint.util;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import java.lang.reflect.Field;

public class ScreenUtil {
    private ScreenUtil() {
    }

    private static int widthPixel = 0;
    private static int heightPixel = 0;

    public static int getScreenWidth(Context context) {
        if (0 == widthPixel || 0 == heightPixel) {
            DisplayMetrics outMetrics = new DisplayMetrics();
            ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRealMetrics(outMetrics);
            widthPixel = outMetrics.widthPixels;
            heightPixel = outMetrics.heightPixels;
        }

        if (0 == widthPixel || 0 == heightPixel) {
            widthPixel = context.getResources().getDisplayMetrics().widthPixels;
        }

        return widthPixel;
    }

    public static int getScreenHeight(Context context) {
        if (0 == widthPixel || 0 == heightPixel) {
            DisplayMetrics outMetrics = new DisplayMetrics();
            ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRealMetrics(outMetrics);
            widthPixel = outMetrics.widthPixels;
            heightPixel = outMetrics.heightPixels;
        }

        if (0 == widthPixel || 0 == heightPixel) {
            heightPixel = context.getResources().getDisplayMetrics().heightPixels;
        }

        return heightPixel;
    }

    public static void setVisibilityBoolen(View view, boolean visibility) {
        if (view != null) {
            if (visibility) {
                view.setVisibility(View.VISIBLE);
            } else {
                view.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dp2px(Context context, float dpValue) {
        if (null == context) {
            return 0;
        }
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param context
     * @param spValue
     * @return
     */
    public static int sp2px(Context context, float spValue) {
        if (null == context) {
            return 0;
        }
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 使用java正则表达式去掉多余的.与0
     *
     * @param s
     * @return
     */
    public static String subZeroAndDot(String s) {
        if (s == null) {
            return "";
        }
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    /**
     * 获取状态栏的高度
     *
     * @param context
     * @return
     */
    public static int getBarHeight(Context context) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, sbar = 38;//默认为38，貌似大部分是这样的
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            sbar = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return sbar;
    }

    /**
     * 根据屏幕宽高 获取较小的值
     */
    public static int getScreenSmallSize(Context context) {
        int screenWidth = getScreenWidth(context);
        int screenHeight = getScreenHeight(context);
        return screenWidth < screenHeight ? screenWidth : screenHeight;
    }

}
